<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1100px">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
        <el-form-item label="物资名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物资名称"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="物资规格" prop="materialSpecification">
          <el-input
            v-model="queryParams.materialSpecification"
            placeholder="请输入物资规格"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClickSearch">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="handleClickReset">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
          <el-button @click="saveValues"> 保存 </el-button>
        </el-form-item>
      </el-form>
      <Table
        :columns="columns"
        :page-param="queryParams"
        :adaptive="true"
        :page-data="queryData"
        save-key="MaterialInfoSelect"
        @page-change="getPage"
        @row-dblclick="handleTableDblclick"
      >
        <template #disable="{ row }">
          {{ row.type == 0 ? '否' : '是' }}
        </template>
      </Table>
    </ContentWrap>
    <ContentWrap class="h-[230px]">
      <Table :columns="columns" :data="selectInfoList" @row-dblclick="handleSelectTableDblclick">
        <template #disable="{ row }">
          {{ row.type == 0 ? '否' : '是' }}
        </template>
      </Table>
    </ContentWrap>
  </Dialog>
</template>
<script setup name="MaterialInfoSelect" lang="ts">
import * as materialInfoApi from '@/api/material/materialInfo'
import * as materialInfoModel from '@/model/material/info/MaterialInfo'
const selectInfoList = ref<materialInfoModel.MaterialInfo[]>([]) // 选中的字段列表
const dialogTitle = ref('')
const dialogVisible = ref(false)
const queryFormRef = ref()
const queryParams = ref({
  materialName: undefined,
  materialSpecification: undefined
})
const queryData = ref()
const emit = defineEmits(['selectValue'])
const columns = [
  {
    label: '物资名称',
    prop: 'materialName'
  },
  {
    label: '物资类别名称',
    prop: 'category.categoryName'
  },
  {
    label: '物资规格',
    prop: 'materialSpecification'
  },
  {
    label: '基本单位',
    prop: 'unit'
  },
  {
    label: '是否停用',
    prop: 'disable',
    slot: 'disable'
  },
  {
    label: '销售单价',
    prop: 'price'
  },
  {
    label: '条码',
    prop: 'barcode'
  },
  {
    label: '物资描述',
    prop: 'materialDescribe'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]

onMounted(() => {
  getPage()
})

/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  selectInfoList.value = []
  handleClickSearch()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  try {
    queryData.value = await materialInfoApi.pageQuery(queryParams.value)
  } finally {
  }
}

/** 双击table列操作  */
const handleTableDblclick = (row) => {
  selectInfoList.value.push(row)
}

const saveValues = () => {
  emit('selectValue', selectInfoList.value)
  close()
}

/** 关闭弹窗 */
const close = async () => {
  dialogVisible.value = false
}

const handleSelectTableDblclick = (row) => {
  const index = selectInfoList.value.indexOf(row)
  selectInfoList.value.splice(index, 1)
}
</script>
